<?php
/**
 * 数据导入
 * @author jianhui
 *
 */
class Import extends MY_Controller{
    function Import(){
        parent::MY_Controller();
        if (!$this->auth->isImport()){
            showError('您无此页面操作权限');
        }
    }
    /**
     * 列表
     */
    function index(){
        $tab = $this->input->get('tab');
        $tab || $tab = '100city';
        $this->viewData['tab'] = $tab;
        //标准动作数据
        $this->load->model('actionModel');
        $where = array();
        //年
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $where['year'] = $year;
        //时间配置
        $this->load->model('timeUnitModel');
        $this->viewData['yearList'] = $this->timeUnitModel->getCacheData('yearList');
        $timeUnitList = $this->timeUnitModel->getCacheData('timeUnitList');
        $this->viewData['timeUnitList'] = $timeUnitList[$year];
        //时间单位
        $timeUnit = (int)$this->input->get('timeUnit');
        $timeUnit && $where['timeUnit'] = $timeUnit;
        $this->viewData['actionList'] = $this->actionModel->getNewList($where,'','','','orderNum asc,id asc');
        $this->viewData['where'] = $where;
        $this->load->view('Import/index', $this->viewData);
    }
    /**
     * 保存文件
     */
    function save(){
        $type = (int)$this->input->post('type');
        $type || showError('请选择导入数据类别');
        $year = (int)$this->input->post('year');
        $year || showError('请选择年份');
        $timeUnit = (int)$this->input->post('timeUnit');
        $timeUnit || showError('请选择双月');
        
        $fileData = $this->upload();
        $fullPath = $fileData['full_path'];
        $handle = fopen ($fullPath, "r");
        $this->load->model('importMoneyModel');
        switch ($type){
            case 1:
                $i=0;
                $ok = 0;
                $err= 0;
                while ($item = fgetcsv($handle, 10000, ",")){
                    if($i==0)
                    {
                        $i++;
                        continue;
                    }
                    $data = array(
                        'type' => $type,
                        'year' => $year,
                        'timeUnit' => $timeUnit,
                        'coding' => $item[5],
                    );
                    if($this->importMoneyModel->add100CityData($data)){
                        $ok ++;
                    }else{
                        $err ++;
                    }
                    usleep(500);
                }
                showSuccess('导入完成：（成功条数：'.$ok.',失败条数：'.$err.'）', printUrl('Import', 'index', array('tab'=>'100city')), 3);
                break;
            case 2:
                $i=0;
                $ok = 0;
                $err= 0;
                while ($item = fgetcsv($handle, 10000, ",")){
                    if($i==0)
                    {
                        $i++;
                        continue;
                    }
                    $data = array(
                        'type' => $type,
                        'year' => $year,
                        'timeUnit' => $timeUnit,
                        'coding' => $item[5],
                        'money' => (float)$item[6],
                    );
                    if($this->importMoneyModel->addTimeUnitData($data)){
                        $ok ++;
                    }else{
                        $err ++;
                    }
                    usleep(500);
                }
                showSuccess('导入完成：（成功条数：'.$ok.',失败条数：'.$err.'）', printUrl('Import', 'index', array('tab'=>'timeUnitMoney')), 3);
                break;
        }
    }
    /**
     * 上传文件
     * @return array
     */
    function upload(){
        //允许上传文件格式
        $config['allowed_types'] = 'csv';
        $extPath = date('Ymd');//子目录
        //上传目录
        $config['upload_path'] = '../upload/'. $extPath .'/';
        //网站目录
        $webPath = substr($config['upload_path'], 2);
        //判断上传目录是否存在，不存在则创建
        if (!is_dir($config['upload_path'])){
            mkdir($config['upload_path'],0777);
        }
        //是否重命名
        $config['encrypt_name'] = TRUE;
        $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload('inputFile'))
        {
            $error = $this->upload->display_errors('','');
            showError($error);
        } else {
            $data = $this->upload->data();
            return $data;
        }
    }
    /**
     * 添加标准动作
     */
    function actionAdd(){
        if ($_POST){
            $this->load->model('actionModel');
            $where = array(
                'year'      => $this->input->post('year'),
                'timeUnit'  => (int)$this->input->post('timeUnit'),
                'name'      => $this->input->post('name'),
                'type'      => (int)$this->input->post('type'),
            );
            $data = array(
                'orderNum'  => (int)$this->input->post('orderNum'),
                'status'    => 1,
                'createTime'=> time()
            );
            $where['year'] || showError('请选择年份');
            $where['timeUnit'] || showError('请选择双月');
            $where['type'] || showError('请选择基金类别');
            $where['name'] || showError('标准动作名称不能为空');
            if ($this->actionModel->getData($where)){
                showError('您已经添加过同名标准动作');
            }
            if ($this->actionModel->add(array_merge($where, $data))){
                showSuccess('添加标准动作成功',printUrl('Import', 'index', array('tab'=>'action')) );
            }else{
                showError('添加失败');
            }
        }else{
            $this->load->model('timeUnitModel');
            $this->viewData['yearList']     = $this->timeUnitModel->getCacheData('yearList');
            $timeUnitList = $this->timeUnitModel->getCacheData('timeUnitList');
            $year = $this->config->item('nowYear');
            $this->viewData['timeUnitList'] = $timeUnitList[$year];
            $this->load->view('Import/actionAdd', $this->viewData);
        }
    }
    /**
     * 修改标准动作
     */
    function actionEdit(){
        $this->load->model('actionModel');
        if($_POST){
            $id = (int)$this->input->post('id');
            $where = array(
                'year'      => $this->input->post('year'),
                'timeUnit'  => (int)$this->input->post('timeUnit'),
                'name'      => $this->input->post('name'),
                'type'      => (int)$this->input->post('type'),
            );
            $data = array(
                'orderNum'  => (int)$this->input->post('orderNum'),
                'status'    => 1,
            );
            $where['year'] || showError('请选择年份');
            $where['timeUnit'] || showError('请选择双月');
            $where['type'] || showError('请选择基金类别');
            if ($this->actionModel->getData(array_merge($where, array('id !=' => $id) ))){
                showError('您已经添加过同名标准动作');
            }
            if ($this->actionModel->edit($id, array_merge($where, $data))){
                showSuccess('修改标准动作成功',printUrl('Import', 'index', array('tab'=>'action')) );
            }else{
                showError('修改失败');
            }
        }else{
            $id = (int)$this->input->get('id');
            $id || showError('请选择要修改标准动作');
            $this->viewData['data'] = $this->actionModel->getData($id);
            //配置
            $this->load->model('timeUnitModel');
            $this->viewData['yearList']     = $this->timeUnitModel->getCacheData('yearList');
            $timeUnitList = $this->timeUnitModel->getCacheData('timeUnitList');
            $year = $this->config->item('nowYear');
            $this->viewData['timeUnitList'] = $timeUnitList[$year];
            $this->load->view('Import/actionEdit', $this->viewData);
        }
    }
    /**
     * 更改标准动作状态
     */
    function actionStatus(){
        $where['id'] = (int)$this->input->post('id');
        $where['id'] || showError('请选择要操作的标准动作');
        $this->load->model('actionModel');
        $findData = $this->actionModel->getData($where);
        $data = array(
            'status' => $findData['status'] == 1 ? 0 : 1,
        );
        if ($this->actionModel->edit($where, $data)){
            showSuccess('更改状态成功');
        }else{
            showError('更改状态失败');
        }
    }
}